using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WindowsApplication4
{
    /// <summary>
    /// A single compaign which has targets associated with it
    /// </summary>
    public class Compaign
    {
        private DataRow drCompaign;
        private CompaignManager compaignManager;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="compaign">Data row in the compaign data table for the compaign</param>
        /// <param name="compaignmanager">Compaign manager containing the data set for everything</param>
        public Compaign(DataRow compaign, CompaignManager compaignmanager)
        {
            this.drCompaign = compaign;
            this.compaignManager = compaignmanager;
        }

        #region Properties
        /// <summary>
        /// Gets or sets the name of the compaign
        /// </summary>
        public string Name
        {
            get
            {
                //Get the name of the compaign from the data row
                return this.drCompaign[CompaignManager.CONSTS.CompaignColumns.NameIndex].ToString();
            }
            set 
            {
                //Set the name in the data row
                this.drCompaign[CompaignManager.CONSTS.CompaignColumns.NameIndex] = value;
            }
        }

        /// <summary>
        /// Gets the list of targets in the compaign
        /// </summary>
        public List<Target> Targets
        {
            get
            {
                //Gets all rows of targets in this compaign
                DataRow[] rows = this.drCompaign.GetChildRows(CompaignManager.CONSTS.Relations.CompaignToTarget);

                //If there are no rows
                if (rows.Length == 0)
                {
                    //Return an empty list
                    return new List<Target>(0);
                }
                else
                {
                    //Create a new list
                    List<Target> targets = new List<Target>(rows.Length);

                    //For each row of targets
                    foreach (DataRow dr in rows)
                    {
                        //Add target to list
                        targets.Add(new Target(dr, this.compaignManager));
                    }

                    //Return list of targets
                    return targets;
                }
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Add a target village to compaign
        /// </summary>
        /// <param name="villageToAttack">Target village being attacked</param>
        /// <param name="targetDescription">Description of target</param>
        /// <returns>Target village being attacked</returns>
        public Target AddTarget(Village villageToAttack, string targetDescription)
        {
            //Add a new row to the data table of targets
            DataRow newTarget = this.compaignManager.dtTargets.Rows.Add(new object[] { null
                , drCompaign[CompaignManager.CONSTS.CompaignColumns.CompaignIDIndex]
                , villageToAttack.ID
            ,targetDescription});

            //Return new target
            return new Target(newTarget, this.compaignManager);
        }

        /// <summary>
        /// Delete compaign and all associated targets
        /// </summary>
        internal void Delete()
        {
            foreach (Target target in this.Targets)
            {
                target.Delete();
            }

            this.drCompaign.Delete();
            //compaigns.dsCompaign.AcceptChanges();
        }
        #endregion
    }
}
